feat: add optional configurable JSX placeholder naming#207
Conversation
There was a problem hiding this comment.
Pull request overview
Adds optional configuration for more stable, human-readable JSX placeholder identifiers generated from <Trans> JSX, aligning the SWC plugin behavior with LinguiJS’ macro feature request.
Changes:
- Introduces
jsxPlaceholderAttributeandjsxPlaceholderDefaultsoptions and threads them into message building. - Updates
MessageBuilderto emit named JSX placeholders (with suffixing for collisions) and strip the naming attribute from generated component AST. - Adds JSX fixture tests and snapshot outputs covering named placeholders, defaults, deduplication, and prop-order scenarios.
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| yarn.lock | Regenerated lockfile (currently switched to Yarn v1 format). |
| tests/jsx.rs | Adds new test cases for named JSX placeholders and dedup/suffix behavior. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_basic.js | Snapshot for _t-driven placeholder naming. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_stripped_ast.js | Snapshot verifying _t attribute stripping from emitted component AST. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_defaults.js | Snapshot for global defaults-based placeholder naming. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_deduplication_different_props.js | Snapshot for suffixing when same placeholder name has different props. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_deduplication_identical.js | Snapshot for deduplication when same placeholder name + identical props repeats. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_deduplication_with_stripped_props.js | Snapshot for deduplication when _t is stripped and props determine equality. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_attribute_ignored_when_not_configured.js | Snapshot ensuring _t is ignored unless configured. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_prop_order.js | Snapshot for behavior when same props appear in different orders. |
| tests/swc_snapshots/tests/jsx.rs/jsx_named_placeholders_prop_order2.js | Snapshot for behavior when props differ. |
| src/options.rs | Adds new options to config struct, normalization, and defaults. |
| src/lib.rs | Passes options into MessageBuilder::parse and simplifies replacer handling. |
| src/js_macro_folder.rs | Passes options into MessageBuilder::parse for JS macro paths. |
| src/builder.rs | Implements placeholder naming logic, dedup/suffixing, and _t attr stripping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Note that this PR hasn't been updated to align with the changes in lingui/js-lingui#2505 - will do so once the final API has been decided. |
|
@mogelbrod could you please align this PR with the final Babel version? Thanks! |
|
@andrii-bodnar I've pushed a commit that aligns this SWC implementation with lingui/js-lingui#2505 |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 20 out of 21 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 20 out of 21 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@mogelbrod thank you! Please also check failing CI/CD pipelines |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #207 +/- ##
==========================================
+ Coverage 92.59% 92.83% +0.24%
==========================================
Files 8 8
Lines 1444 1578 +134
==========================================
+ Hits 1337 1465 +128
- Misses 107 113 +6
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
@mogelbrod It looks like the |
Thanks! I've checked out the version from |
Description
SWC implementation of lingui/js-lingui#2505
Add a new feature that allows developers to customize the identifiers of JSX placeholders generated by the
<Trans>macro. This results in placeholder identifiers in extracted messages that are more stable, human-readable, and contextual, instead of the default arbitrary numeric indices (e.g., replacing<0>with<em>or<link>).It is accomplished via two new optional configuration options (via
lingui.config.jsin themacrooptions):jsxPlaceholderAttribute: Allows defining a custom prop name (e.g.,_t) that can be used on components to set an explicit placeholder name locally.jsxPlaceholderDefaults: Allows providing a record of tag names to placeholder names, enabling global default renaming for specific JSX components.Types of changes
Fixes #1188
Checklist
Disclaimer
Much of this PR was generated by Copilot using the Gemini 3.1 Pro (Preview) model with training/sharing disabled.